<?php

class AnalyzeError extends ErrorMessage
{
	var $cols = array();
	var $error = array();
	var $errorDesc = array();
	var $values;
	var $cntCol;
	var $fieldSep = '/\|/';

	function initCols()
	{
		reset($this->error);
		$tempCols = array_shift($this->error);
		$this->cntCol = 0;
		$this->cols = array();
		$colsSep = preg_split( $this->fieldSep, $tempCols);
		
		foreach( $colsSep as &$col )
		{
			$this->cols[ strtoupper($col) ] = $this->cntCol;
			$this->cntCol++;
		}
	}
	
	function pushRow($src, $msg = '%s', &$ary)
	{
		$exit = count( $this->error );
		$cRow = 0;
		$aRow = array();
		$row = "";
		$tmpValue = '';
		while( $exit-- )
		{
			$tmpValueCols = preg_split( $this->fieldSep, $this->error[ $cRow ]);
			if ( strtoupper( $tmpValueCols[ $this->cols[ strtoupper('campo') ] ] ) == strtoupper( $src ) )
			{
				$tmpValue = $this->errDesc( $tmpValueCols[ $this->cols[ strtoupper('id') ] ] );
				if ( $tmpValue != '' ) 
					$ary[] = sprintf($msg ,$tmpValue);
				else
					$ary[] = $tmpValueCols[ $this->cols[ strtoupper('descrizione') ] ];
				$aRow[] = $cRow;
			}
			$cRow++;
		}
		foreach( $aRow as $val )
		{
			unset( $this->error[ $val ] );
		}
	}

	function pushAllRow(&$ary)
	{
		$exit = count( $this->error );
		$cRow = 0;
		$row = "";
		$tmpValue = '';
		foreach( $this->error as $val )
		{
			$tmpValueCols = preg_split( $this->fieldSep, $val );
			$tmpValue = $this->errDesc( $tmpValueCols[ $this->cols[ strtoupper('id') ] ] );
			if ( $tmpValue != '' ) 
				$ary[] = $tmpValue;
			else if ( $tmpValueCols[ $this->cols[ strtoupper('id') ] ] == '')
				$ary[] = $tmpValueCols[ $this->cols[ strtoupper('descrizione') ] ];
			else
				$ary[] = $tmpValueCols[ $this->cols[ strtoupper('id') ] ].' - '.$tmpValueCols[ $this->cols[ strtoupper('descrizione') ] ];
		}
	}

	function checkResponse( &$resp, &$stack )
	{
		$stack = array();
		if ( preg_match("/^ERROR/", $resp) )
		{
			list ($tmp, $errNum) = split( ':' , $resp);
			$stack[] = $this->errDesc( $errNum );
			return true;
		}
		return false;
	}

	function chomp(&$string) 
	{ 
		$endchar = substr("$string", strlen("$string") - 1, 1); 
		if ($endchar == "\n") 
		{ 
				$string = substr("$string", 0, -1); 
		} 
	} 	
}

?> 
